package com.legendshop.openportal.sdk.service;

import com.legendshop.openportal.sdk.client.ClientExecutionParams;
import com.legendshop.openportal.sdk.client.OpenClient;
import com.legendshop.openportal.sdk.client.http.HttpResponseAnalyzer;
import com.legendshop.openportal.sdk.client.http.HttpResponseAnalyzerSupport;
import com.legendshop.openportal.sdk.client.http.HttpResponseHandler;
import com.legendshop.openportal.sdk.client.http.handler.DefaultHttpResponseHandler;
import com.legendshop.openportal.sdk.model.BaseRequest;
import com.legendshop.openportal.sdk.model.OpenResult;
import com.legendshop.openportal.sdk.registry.OpenClientRegistry;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 开放平台基础服务
 * @author jzh
 * @version 1.0.0
 * @title BaseService
 * @date 2024/6/28 17:16
 * @description：
 */
public class BaseService {

    /**
     * 通用的执行请求实现
     * @param request
     * @param type
     * @return
     * @param <input>
     * @param <output>
     */
    public static <input extends BaseRequest, output> OpenResult<output> execute(BaseRequest request, Type type) {
        return execute(request, type, HttpResponseAnalyzerSupport.getAnalyzerChain());
    }

    /**
     * 通用的执行请求实现
     * @param request
     * @param type
     * @param analyzerList
     * @return
     * @param <input>
     * @param <output>
     */
    public static <input extends BaseRequest, output> OpenResult<output> execute(BaseRequest request, Type type, List<HttpResponseAnalyzer> analyzerList) {
        HttpResponseHandler<output> responseHandler = new DefaultHttpResponseHandler<>(type, analyzerList);
        return execute(new ClientExecutionParams<>(true, request, responseHandler));
    }

    /**
     * 通用的执行请求实现
     * @param clientExecutionParams
     * @return
     * @param <input>
     * @param <output>
     */
    public static <input extends BaseRequest, output> OpenResult<output> execute(ClientExecutionParams<input, output> clientExecutionParams) {
        OpenClient openClient = OpenClientRegistry.getOpenClient();
        return openClient.execute(clientExecutionParams);
    }
}
